package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.BussinessLevel;
import com.sl.au.entity.Division;
import com.sl.au.entity.Evaluate;
import com.sl.au.entity.Role;
import com.sl.au.service.EvaluateService;
import com.sl.au.service.BusinesssellerBaseInfoService;
import com.sl.au.service.BussinessLevelService;
import com.sl.au.service.DivisionService;
import com.sl.au.service.OptionService;
import com.sl.au.service.RoleService;
@RestController
@RequestMapping(value = "/ui/businessshow")
public class BusinesssellerBaseInfoShowController extends BaseController{

	@Autowired
	private BusinesssellerBaseInfoService businesssellerBaseInfoService;
	@Autowired
	private EvaluateService evaluateService;
    @Autowired
    private OptionService optionService;
    
    @Autowired
    private DivisionService divisionService;
    
    @Autowired
	BussinessLevelService bussinessLevelService;
    
    @Autowired
    RoleService roleService;
    
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView, HttpServletRequest request) {

			modelAndView=this.createLayoutView("businessseller/businesssellerBaseInfoShow");
		return modelAndView;
	}
	
	@RequestMapping(value = "getbaseinfopage", method = RequestMethod.GET)
	public Map<String, Object> getbaseinfopage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String level=request.getParameter("level");
		Role role = roleService.getRepository().findByName("商户");
		//List<BusinesssellerBaseInfo> businesssellerBaseInfo = this.businesssellerBaseInfoService.getPage(start, length, key, "createTime", Sort.Direction.DESC);
		List<BusinesssellerBaseInfo> businesssellerBaseInfo = this.businesssellerBaseInfoService.getPage(start, length, key, level, role.getId(), "createTime", Sort.Direction.DESC);
		int count = businesssellerBaseInfoService.getCount(key, level,role.getId());
		map.put("data", businesssellerBaseInfo);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getBussinesserInfo/{identify}", method = RequestMethod.GET)
	public Map<String, Object> getBussinesserInfo(@PathVariable String identify) {
		Map<String, Object> map = new HashMap<String, Object>();
		BusinesssellerBaseInfo businesssellerBaseInfo= this.businesssellerBaseInfoService.getRepository().findOne(identify);
		map.put("businesssellerBaseInfo", businesssellerBaseInfo);
		Evaluate evaluate= evaluateService.getRepository().getInfoByBusinessseller(businesssellerBaseInfo.getId());
		map.put("evaluate", evaluate);
		/*map.put("recordsFiltered", count);
		map.put("recordsTotal", count);*/
		return map;
	}
	
	//显示商户的所有节点和商户图片ID
	@RequestMapping(value="getBussinessLevelAllNodes",method = RequestMethod.GET)
	public Map<String,Object> getBussinessLevelAllNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
//		List<BussinessLevel> node=null;
//		node=bussinessLevelService.getAllNodes();
//		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
//		nodeMap=bussinessLevelService.getNodeMap(node);
		List<Division> node = null;
		node = divisionService.getAllNodes();
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
		nodeMap=divisionService.getNodeMap(node);
		map.put("code", 1);
		map.put("nodes", nodeMap);
		return map;
	}
}
